﻿using System;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.AppLaunch.Common.Security;
using XuanFe.Database.BLL;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class ChangePwdForm : DevExpress.XtraEditors.XtraForm
    {
        private const string m_ModuleId = "0103";

        public ChangePwdForm()
        {
            InitializeComponent();
        }
        private bool ValidityCheck()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (Global.Instance.MandatoryFieldEmpty(this.txtOldPassword, false, ref message))
            {
                this.errorProvider1.SetError(this.txtOldPassword, message);
                this.txtOldPassword.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtNewPassword, false, ref message))
            {
                this.errorProvider1.SetError(this.txtNewPassword, message);
                this.txtNewPassword.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtNewPassword2, false, ref message))
            {
                this.errorProvider1.SetError(this.txtNewPassword2, message);
                this.txtNewPassword2.Focus();
                return (false);
            }
            if (!string.Equals(this.txtNewPassword.Text, this.txtNewPassword2.Text))
            {
                this.errorProvider1.SetError(this.txtNewPassword2, "两次的新密码不一致");
                this.txtNewPassword2.Focus();
                return (false);
            }

            return (true);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheck()) return;

                string staffId = LoginInfo.Instance.StaffLogon.Staff.Id;
                string oldPassword = CryptographyHelper.Instance.MD5(string.Concat(staffId, this.txtOldPassword.Text.Trim()));
                string password = CryptographyHelper.Instance.MD5(string.Concat(staffId, this.txtNewPassword.Text.Trim()));

                StaffModel staff = StaffDAL.Instance.Get(LoginInfo.Instance.StaffLogon.Staff.Id);
                if (staff == null)
                {
                    // 账号不存在
                    Global.Instance.ErrorDlg("账号不存在");
                    return;
                }
                if (!string.Equals(staff.Password, oldPassword, StringComparison.OrdinalIgnoreCase))
                {
                    // 密码不正确
                    Global.Instance.ErrorDlg("旧密码不正确");
                    return;
                }

                StaffModel model = new StaffModel() { Id = staffId, Password = password };
                int r = StaffBLL.Instance.UpdatePassword(model);
                if (r != 0)
                {
                    Global.Instance.ErrorDlg("修改密码失败");
                    return;
                }
                LoginInfo.Instance.StaffLogon.Staff.Password = password;
                StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_ModuleId, "修改密码成功");

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}
